/**
 * Created by wlc on 2018/5/7.
 */
//synapse-args
var isSynapse;  //判断是否处于画线状态，1是，0不是
var SynapseStatus; //判断画线是否开始，1开始，0不开始
var beginSynapse;  //画线开始元素的ID
var beginSynapsePoint;    //画线开始元素的输入输出，A输入，B输出
var beginSynapseX;
var beginSynapseY;
var SynapseCanvas;
var SynapseContext;
var SynapseArrA;     //存储同一元素输入方的不同突触
var SynapseArrB;     //存储同一元素输出方的不同突触

//menu-args
var menuUnfold;     //判断菜单栏是否展开，1是，0不是

/**
 * 菜单栏-内容展开收缩
 */
$('.menuBar-list').click(function (e) {
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    var title = tar.id.split("-")[1];
    if (menuUnfold === 1){
        $('#'+title+"-content")[0].style.display = "";
        menuUnfold = 0;
    }else {
        $('#'+title+"-content")[0].style.display = "block";
        menuUnfold = 1;
    }
});

/**
 * 菜单栏-Jarvis-新建-神经网络
 */
$('#Jarvis-new-NN').click(function (e) {
    $.ajax({
        type: 'POST',
        url: '../NeuralNetworks/newNeuralNetworks' ,
        success:function (NeuralNetworksName) {
            $('#config-title')[0].children[0].innerText = NeuralNetworksName;
            var panel = $(panel_NN)[0];
            panel.id = NeuralNetworksName;
            $("#model-container")[0].appendChild(panel);
        }
    });
});

/**
 * 菜单栏-视图-模型工具
 */
$('#view-modelTools').click(function (e) {
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    if (tar.children[0].style.display === "") {
        $('#model-tools')[0].style.display = "block";
        tar.children[0].style.display = "block";
    }else {
        $('#model-tools')[0].style.display = "";
        tar.children[0].style.display = "";
    }
});

/**
 * 菜单工具栏窗口-配置按钮
 */
$('#config-item').click(function (e) {
    var config_content = $('#config-content')[0];
    if(config_content.style.display === ""){
        config_content.style.display = "block";
    }else {
        config_content.style.display = "";
    }
});

/**
 * 菜单工具栏窗口-配置按钮-设置配置项
 */
$('#config-content-tools').click(function (e) {
    $('#config-tools')[0].style.display = "block";
});

/**
 * 菜单工具栏窗口-运行按钮
 */
$('#button-run').click(function (e) {
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素
    var NeuralNetworksName = $('#config-title')[0].children[0].innerText;

    $('#button-stop')[0].children[0].children[0].style.color = "#F34444";
    $.ajax({
        type: 'POST',
        url: '../NeuralNetworks/run' ,
        data: {NeuralNetworksName:NeuralNetworksName},
        success:function (returndata) {
            $('#button-stop')[0].children[0].children[0].style.color = "";
            console.log(returndata)
        }
    });
});

/**
 * 菜单工具栏窗口-展开与收缩按钮
 */
$('#button-collapse').click(function (e) {
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    if (tar.className.indexOf("up") !== -1){
        tar.className = "collapse down";
        $('#menu-menuBar')[0].style.marginTop = "-30px";
    }else {
        tar.className = "collapse up";
        $('#menu-menuBar')[0].style.marginTop = "";
    }
});

/**
 * 配置工具栏-神经网络-按钮-确定
 */
$('#NNconfig-button-confirm').click(function (e) {
    $('#config-tools')[0].style.display = "";
});

/**
 * 配置工具栏-神经网络-按钮-取消
 */
$('#NNconfig-button-cancel').click(function (e) {
    $('#config-tools')[0].style.display = "";
});

/**
 * 配置工具栏-神经网络-按钮-应用
 */
$('#NNconfig-button-apply').click(function (e) {
});

/**
 * 函数工具栏-函数选择
 */
$('.function-item').click(function (e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素
    var NeuralNetworksName = $('#config-title')[0].children[0].innerText;

    var functionId;
    var neuronsId = $('#function-model-id')[0].value;
    if (tar.className === "function-title" || tar.className === "function-content"){
        functionId = tar.parentNode.id;
    }else if (tar.className === "function-formula"){
        functionId = tar.parentNode.parentNode.id;
    }
    $('#function-tools')[0].style.display = "";
    $.ajax({
        type: 'POST',
        url: '../NeuralNetworks/ActivationAdd' ,
        data: {NeuralNetworksName:NeuralNetworksName,neuronsId:neuronsId,functionId:functionId}
    });
    //添加图标
    var neuronsModel = $('#'+neuronsId)[0];
    var functionIcon;
    for (var i=0;i<neuronsModel.children.length;i++){
        if (neuronsModel.children[i].className==="function-icon"){
            neuronsModel.removeChild($('#'+ neuronsModel.id + "functionIcon")[0])
        }
    }
    switch (functionId){
        case "function-constant":
            functionIcon = $(ICONCONSTANT)[0];
            break;
        case "function-sigmod":
            functionIcon = $(ICONSIGMOD)[0];
            break;
    }
    functionIcon.id = neuronsModel.id + "functionIcon";
    neuronsModel.appendChild(functionIcon);
});

/**
 * 扇形工具栏-画线按钮
 */
$(document).on("click",'.sector-tools-synapse', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    var sectorLis;
    var neurons =  $(tar).parents('.neurons')[0];
    isSynapse = 1;
    beginSynapse = neurons.id;
    neurons.children[0].children[0].style.cursor = "crosshair";
    neurons.children[0].children[1].style.cursor = "crosshair";
    sectorLis = $(tar).parents('ul')[0].children;
    for (var i=1;i<sectorLis.length;i++){
        sectorLis[i].style.transform = "rotate(-90deg)";
    }
    setTimeout("$('#"+$(tar).parents('.sector-tools')[0].id+"').remove();",500);
});

/**
 * 扇形工具栏-函数按钮
 */
$(document).on("click",'.sector-tools-function', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    var sectorLis;
    $('#function-tools')[0].style.display = "block";
    $('#function-model-id')[0].value = $(tar).parents('.neurons')[0].id;
    sectorLis = $(tar).parents('ul')[0].children;
    for (var i=1;i<sectorLis.length;i++){
        sectorLis[i].style.transform = "rotate(-90deg)";
    }
    setTimeout("$('#"+$(tar).parents('.sector-tools')[0].id+"').remove();",500);
});

/**
 * 数据面板-侧边栏-文件列表-通过判断文件选中使打开按钮可用
 */
$(document).on("click",'.panel-DATA .panel-DATA-sidebar .panel-DATA-fileList', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    if ((tar.tagName === "SPAN" && tar.parentNode.children.length === 1) || (tar.tagName === "DIV" && tar.children.length === 1)){
        var openButon = $(".normal-panel .panel-DATA-openButton")[0];
        openButon.className =  openButon.className.replace("forbidden","select")
    }else {
        var openButon = $(".normal-panel .panel-DATA-openButton")[0];
        openButon.className =  openButon.className.replace("select","forbidden")
    }
});

/**
 * 数据面板-侧边栏-文件列表-文件目录选中
 */
$(document).on("click",'.panel-DATA .panel-DATA-sidebar .panel-DATA-fileItem', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    var fileItem = $(".normal-panel .panel-DATA-fileItem");
    for (var i = 0;i<fileItem.length;i++){
        fileItem[i].style.backgroundColor = "";
    }
    if (tar.tagName.indexOf("SPAN") !== -1){
        tar.parentNode.style.backgroundColor = "#DDDCDC";
    }else if (tar.tagName.indexOf("DIV") !== -1){
        tar.style.backgroundColor = "#DDDCDC";
    }
});

/**
 * 数据面板-侧边栏-文件列表-目录选择-目录展开
 */
$(document).on("click",'.panel-DATA .panel-DATA-sidebar .fa-caret-right', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    tar.className = tar.className.replace("fa-caret-right","fa-caret-down");
    var item = tar.parentNode.parentNode;
    var fileDir = "";
    var li = item;
    while ($(li).parents('li')[0] !== undefined){
        li = $(li).parents('li')[0];
        fileDir = li.children[0].innerText + "/" + fileDir;
    }
    $.ajax({
        type: 'GET',
        url: '../DataPanel/listDirFiles' ,
        data:{filePath:fileDir+item.children[0].innerText},
        success:function (fileAttr) {
            var jsonData = eval('(' + fileAttr + ')');
            var filePath = jsonData.fileNames.split("|");
            var isDir = jsonData.isDirs.split("|");

            var fileList = $('<ul></ul>')[0];
            item.appendChild(fileList);
            for (var i=0;i<filePath.length;i++){
                if (isDir[i] === "1"){
                    fileList.appendChild($('<li><div class="panel-DATA-fileItem"><i class="fa fa-caret-right fa-fw"></i><span>'+filePath[i]+'</span></div></li>')[0]);
                }else {
                    fileList.appendChild($('<li style="padding-left: 20px"><div class="panel-DATA-fileItem"><span>'+filePath[i]+'</span></div></li>')[0]);
                }
            }
        }
    });
});

/**
 * 数据面板-侧边栏-文件列表-目录选择-目录收缩
 */
$(document).on("click",'.panel-DATA .panel-DATA-sidebar .fa-caret-down', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    var item = tar.parentNode.parentNode;
    tar.className = tar.className.replace("fa-caret-down","fa-caret-right");
    item.lastChild.remove();
});

/**
 * 数据面板-侧边栏-文件列表-打开按钮-文件选中后打开文件
 */
$(document).on("click",'.panel-DATA .panel-DATA-sidebar .panel-DATA-list .select', function(e){
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    $.ajax({
        type: 'GET',
        url: '../DataPanel/listFilesInfor' ,
        data:{filePath:fileDir+item.children[0].innerText},
        success:function (fileAttr) {

        }
    });
});

/**
 * 通用
 * @param e
 */
document.onclick = function (e) {
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    // //点击除开始元素以外其他区域则取消画线
    // if(tar.parentNode.parentNode.id !== beginSynapse && isSynapse === 1){
    //     $('#'+beginSynapse)[0].children[0].children[0].style.cursor = "";
    //     $('#'+beginSynapse)[0].children[0].children[1].style.cursor = "";
    //     beginSynapse = "";
    //     isSynapse = 0;
    // }
};

/**
 * 标签页切换
 */
$('.tab-title').click(function (e) {
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素

    var lis = tar.parentNode.parentNode.children;
    var tit;
    var tab_pane;
    for (var i = 0; i < lis.length;i++){
        if (lis[i].children[0] === tar){
            lis[i].className = lis[i].className.indexOf("active")!==-1?lis[i].className:lis[i].className+" active";
            tit = lis[i].children[0].dataset.tit;
            tab_pane = $('#'+tit)[0];
            tab_pane.className = tab_pane.className.indexOf("active")!==-1?tab_pane.className:tab_pane.className+" active";
        }else {
            lis[i].className = lis[i].className.indexOf("active")!==-1?lis[i].className.slice(0,-7):lis[i].className;
            tit = lis[i].children[0].dataset.tit;
            tab_pane = $('#'+tit)[0];
            tab_pane.className = tab_pane.className.indexOf("active")!==-1?tab_pane.className.slice(0,-7):tab_pane.className;
        }
    }
});